package com.ruoyi.detail.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.detail.domain.Detail;
import com.ruoyi.detail.service.IDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 明细Controller
 * 
 * @author ucu
 * @date 2024-05-04
 */
@RestController
@RequestMapping("/detail/detail")
public class DetailController extends BaseController
{
    @Autowired
    private IDetailService detailService;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询明细列表
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(Detail detail)
    {
        startPage();
        //只能查看自己的
        detail.setUserId(SecurityUtils.getUserId());
        List<Detail> list = detailService.selectDetailList(detail);
        return getDataTable(list);
    }

    /**
     * 导出明细列表
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:export')")
    @Log(title = "明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Detail detail)
    {
        //只能查看自己的
        detail.setUserId(SecurityUtils.getUserId());
        List<Detail> list = detailService.selectDetailList(detail);
        ExcelUtil<Detail> util = new ExcelUtil<Detail>(Detail.class);
        util.exportExcel(response, list, "明细数据");
    }

    /**
     * 获取明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Detail detail = detailService.selectDetailById(id);
        if (detail != null)
        {
            if (Objects.equals(detail.getUserId(), SecurityUtils.getUserId())){
                return success(detail);
            }
        }
        return success(null);
    }

    /**
     * 新增明细
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:add')")
    @Log(title = "明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Detail detail)
    {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            detail.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            detail.setPicture(null);
        }
        detail.setUserId(SecurityUtils.getUserId());
        return toAjax(detailService.insertDetail(detail));
    }

    /**
     * 修改明细
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:edit')")
    @Log(title = "明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Detail detail)
    {
        if (!("true".equals(configService.selectConfigByKey("info.image.upload")))) {
            detail.setPicture(null);
        }
        if (!("true".equals(configService.selectConfigByKey("info.image.url")))) {
            detail.setPicture(null);
        }
        // 重看,好像没有必要，如果需要修改，直接动就行，需要添加重看的，直接添加就行
        /***
         * SELECT
         *   CASE
         *     WHEN EXISTS (
         *       SELECT 1
         *       FROM detail
         * 			where user_id = 1
         *       GROUP BY name2, season
         *       HAVING COUNT(*) > 1
         *     ) THEN 'true'
         *     ELSE 'false'
         *   END AS has_duplicate;
         ***/
        //        if (detail.getStatus()==3){
        //            // 如果存在数据，新建一个
        //            if (detailService.hasDuplicateWithSeasonAndNameTwo(detail.getUserId())){
        //                detail.setId(null);
        //            }
        //        }
        if (Objects.equals(detail.getUserId(), SecurityUtils.getUserId())){
            return toAjax(detailService.updateDetail(detail));
        }

        return toAjax(0);
    }

    /**
     * 删除明细
     */
    @PreAuthorize("@ss.hasPermi('detail:detail:remove')")
    @Log(title = "明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(detailService.deleteDetailByIdWithUserId(ids));
    }
}
